<template>
  <div class="ck-container">
    <h2>已上传视频</h2>
    <div v-if="videos.length === 0" class="no-video">暂无视频</div>
    <div v-else class="video-grid">
      <div v-for="(video, index) in videos" :key="index" class="video-card">
        <p class="video-name">{{ video.name }}</p>
        <!-- 新增：显示备注 -->
        <p v-if="video.remark" class="video-remark">{{ video.remark }}</p>
        <!-- 视频缩略图或封面 -->
        <video :src="video.url" controls width="100%"></video>
        <div class="video-thumbnail" @click="openVideoModal(video)">
          <span>点击播放视频</span>
        </div>
      </div>
    </div>

    <!-- 遮罩层 -->
    <div v-if="isModalVisible" class="modal-overlay" @click.self="closeVideoModal">
      <div class="modal-content">
        <video :src="currentVideo.url" controls autoplay width="100%"></video>
        <button class="close-btn" @click="closeVideoModal">关闭</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    // 初始状态保存为常量
    const initialVideos = [];

    return {
      videos: [...initialVideos],
      initialVideos,
      isModalVisible: false,
      currentVideo: null
    };
  },
  mounted() {
    this.loadVideos();
    window.addEventListener('storage', this.handleStorageChange);

    // 添加 beforeunload 监听器
    window.addEventListener('beforeunload', this.handleBeforeUnload);
  },
  beforeUnmount() {
    // 移除监听器防止内存泄漏
    window.removeEventListener('storage', this.handleStorageChange);
    window.removeEventListener('beforeunload', this.handleBeforeUnload);
  },
  methods: {
    loadVideos() {
      const storedVideos = JSON.parse(localStorage.getItem('uploadedVideos')) || [];
      this.videos = storedVideos;
    },
    handleStorageChange(event) {
      if (event.key === 'uploadedVideos') {
        this.loadVideos(); // 数据变化时重新加载
      }
    },
    handleBeforeUnload(e) {
      // 恢复初始状态
      this.videos = [...this.initialVideos];

      // 清除 localStorage 中的 uploadedVideos 记录
      localStorage.removeItem('uploadedVideos');
    },
    openVideoModal(video) {
      this.currentVideo = video;
      this.isModalVisible = true;
    },
    closeVideoModal() {
      this.isModalVisible = false;
      this.currentVideo = null;
    }
  }
};
</script>

<style scoped>
.ck-container {
  padding: 20px;
  max-width: 1200px;
  margin: auto;
}

.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
  margin-top: 20px;
}

.video-card {
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  background-color: #f9f9f9;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: transform 0.2s ease;
  cursor: pointer;
}

.video-card:hover {
  transform: translateY(-5px);
}

.video-name {
  font-size: 16px;
  margin-bottom: 10px;
  color: #333;
}

.no-video {
  text-align: center;
  font-size: 18px;
  color: #888;
  margin-top: 20px;
}

.video-remark {
  font-size: 14px;
  color: #666;
  margin-bottom: 10px;
}

.video-thumbnail {
  cursor: pointer;
  text-align: center;
  padding: 20px;
  background-color: #eaeaea;
  border-radius: 4px;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 999;
}

.modal-content {
  padding: 20px;
  border-radius: 8px;
  position: relative;
  max-width: 1000px;
  width: 100%;
}

.close-btn {
  margin-top: 10px;
  padding: 8px 16px;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>